﻿using AMatysiak.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Forms;

namespace AMatysiakTest.Controls
{
    [TestClass]
    public class YearSelectorTest
    {
        [TestMethod]
        public void checkDefaultYear()
        {
            YearSelector ys = new YearSelector();
            Assert.AreEqual(DateTime.Now.Year, ys.Year);
        }

        [TestMethod]
        public void checkDefaultBorderYears()
        {
            YearSelector ys = new YearSelector();
            Assert.AreEqual(1900, ys.MinYear);
            Assert.AreEqual(2100, ys.MaxYear);
        }

        [TestMethod]
        public void setValue()
        {
            YearSelector ys = new YearSelector();
            ys.Year = 1988;

            Assert.AreEqual(1988, ys.Year);
        }

        [TestMethod]
        public void valueChangedEventRaise()
        {
            bool beInside = false;

            YearSelector ys = new YearSelector();
            ys.Year_ValueChanged += delegate 
            {
                Assert.AreEqual(2009, ys.Year);
                beInside = true;
            };
            ys.Year = 2009;

            if(!beInside)
                Assert.Fail("Year_ValueChanged should be raised and handled.");
        }

        [TestMethod]
        public void valueChangedEventRaiseOnDecrement()
        {
            bool beInside = false;

            YearSelector ys = new YearSelector();
            ys.Year = 2009;

            ys.Year_ValueChanged += delegate
            {
                Assert.AreEqual(2008, ys.Year);
                beInside = true;
            };

            Control[] cntrl = ys.Controls.Find("left", true);
            if (cntrl.Length > 0)
            {
                Button btn = (Button)cntrl[0];
                btn.PerformClick();
            }

            if (!beInside)
                Assert.Fail("Year_ValueChanged should be raised and handled.");
        }

        [TestMethod]
        public void valueChangedEventRaiseOnIncrement()
        {
            bool beInside = false;

            YearSelector ys = new YearSelector();
            ys.Year = 2009; 

            ys.Year_ValueChanged += delegate
            {
                Assert.AreEqual(2010, ys.Year);
                beInside = true;
            };

            Control[] cntrl = ys.Controls.Find("right", true);
            if (cntrl.Length > 0)
            {
                Button btn = (Button)cntrl[0];
                btn.PerformClick();
            }

            if (!beInside)
                Assert.Fail("Year_ValueChanged should be raised and handled.");
        }

        [TestMethod]
        public void changeYearInTextBox()
        {
            bool beInside = false;

            YearSelector ys = new YearSelector();
            ys.Year = 2000;

            ys.Year_ValueChanged += delegate
            {
                Assert.AreEqual(2010, ys.Year);
                beInside = true;
            };

            Control[] cntrl = ys.Controls.Find("yearBox", true);
            if (cntrl.Length > 0)
            {
                TextBox tb = (TextBox)cntrl[0];
                tb.Text = "2010";
            }

            if (!beInside)
                Assert.Fail("Year_ValueChanged should be raised and handled.");
        }
    }
}
